package com.bruce.learnrecycler.kotlin.ui.custom

import android.content.Context
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.StaggeredGridLayoutManager
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.view.animation.GridLayoutAnimationController

/**
 * Created by Bruce-Home on 2015/9/16.
 */
public class GridRecyclerView: RecyclerView {
    public constructor(context:Context):super(context) {
    }
    public constructor(context: Context, attrs:AttributeSet): super(context, attrs) {
    }
    public constructor(context:Context, attrs:AttributeSet, defStyleAttr:Int):super(context, attrs, defStyleAttr)

    override fun attachLayoutAnimationParameters(child: View?, params: ViewGroup.LayoutParams, index: Int, count: Int) {
       // super.attachLayoutAnimationParameters(child, params, index, count)
        var aniParams = params.layoutAnimationParameters as? GridLayoutAnimationController.AnimationParameters
        if (aniParams == null) {
            aniParams = GridLayoutAnimationController.AnimationParameters()
            params.layoutAnimationParameters = aniParams
        }
        val columns = (getLayoutManager() as StaggeredGridLayoutManager).getSpanCount()
        aniParams.count = count
        aniParams.index = index
        aniParams.columnsCount = columns
        aniParams.rowsCount = count / columns
        val invertedIndex = count - 1 - index
        aniParams.column = columns - 1 - (invertedIndex % columns)
        aniParams.row = aniParams.rowsCount - 1 - invertedIndex / columns

    }
}